package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportInvoiceRpcService;
import com.hunttown.mes.rpc.domain.TransportInvoiceDTO;
import com.hunttown.mes.rpc.domain.query.TransportInvoiceDTOQuery;
import com.hunttown.mes.service.TransportInvoiceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportInvoiceConverts.convert;

/**
 * created by wangjunfu on 2019-07-07 10:50:11
 */
@Service
public class TransportInvoiceRpcServiceImpl implements TransportInvoiceRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportInvoiceRpcServiceImpl.class);

    private final TransportInvoiceService transportInvoiceService;

    @Autowired
    public TransportInvoiceRpcServiceImpl(TransportInvoiceService transportInvoiceService) {
        this.transportInvoiceService = transportInvoiceService;
    }

    @Override
    public TransportInvoiceDTO insert(TransportInvoiceDTO transportInvoiceDTO) {
        try {
            return convert(transportInvoiceService.insert(convert(transportInvoiceDTO)));
        } catch (Exception e) {
            logger.error("TransportInvoice create failed.", e);
            throw new RuntimeException("TransportInvoice create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportInvoiceDTO transportInvoiceDTO) {
        try {
            return transportInvoiceService.updateInfoById(convert(transportInvoiceDTO));
        } catch (Exception e) {
            logger.error("TransportInvoice update by id failed.", e);
            throw new RuntimeException("TransportInvoice update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportInvoiceDTOQuery transportInvoiceDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportInvoiceDTOQuery.getW_id() != null && transportInvoiceDTOQuery.getW_id() > 0) {
                query.put("w_id", transportInvoiceDTOQuery.getW_id());
            }

            if (transportInvoiceDTOQuery.getIds() != null && !transportInvoiceDTOQuery.getIds().equals("")) {
                query.put("ids", transportInvoiceDTOQuery.getIds());
            }

            if (transportInvoiceDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportInvoiceDTOQuery.getStopFlag());
            }

            if (transportInvoiceDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportInvoiceDTOQuery.getDeleteFlag());
            }

            return transportInvoiceService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportInvoice update by query failed.", e);
            throw new RuntimeException("TransportInvoice update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportInvoiceDTO selectById(Integer id) {
        try {
            return convert(transportInvoiceService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportInvoice select by id failed.", e);
            throw new RuntimeException("TransportInvoice select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportInvoiceDTO selectObjByQuery(TransportInvoiceDTOQuery transportInvoiceDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportInvoiceDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportInvoiceDTOQuery.getStopFlag());
            }

            if (transportInvoiceDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportInvoiceDTOQuery.getDeleteFlag());
            }

            return convert(transportInvoiceService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportInvoice select obj by query failed.", e);
            throw new RuntimeException("TransportInvoice select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportInvoiceDTO> getListForPage(TransportInvoiceDTOQuery transportInvoiceDTOQuery) {
        Query query = Query.page(transportInvoiceDTOQuery.getPn(), transportInvoiceDTOQuery.getPageSize());

        if (transportInvoiceDTOQuery.getId() != null && transportInvoiceDTOQuery.getId() > 0) {
            query.put("id", transportInvoiceDTOQuery.getId());
        }

        if (transportInvoiceDTOQuery.getIds() != null && !transportInvoiceDTOQuery.getIds().equals("")) {
            query.put("ids", transportInvoiceDTOQuery.getIds());
        }

        if (transportInvoiceDTOQuery.getTableId() != null && transportInvoiceDTOQuery.getTableId() > 0) {
            query.put("tableId", transportInvoiceDTOQuery.getTableId());
        }

        if (transportInvoiceDTOQuery.getRecordId() != null && transportInvoiceDTOQuery.getRecordId() > 0) {
            query.put("recordId", transportInvoiceDTOQuery.getRecordId());
        }

        if (transportInvoiceDTOQuery.getStatus() != null) {
            query.put("status", transportInvoiceDTOQuery.getStatus());
        }

        if (StringUtils.isNotBlank(transportInvoiceDTOQuery.getLikeName())) {
            query.put("likeName", transportInvoiceDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportInvoiceDTOQuery.getRemarkLike())) {
            query.put("remarkLike", transportInvoiceDTOQuery.getRemarkLike());
        }

        if (StringUtils.isNotBlank(transportInvoiceDTOQuery.getLikeCode())) {
            query.put("likeCode", transportInvoiceDTOQuery.getLikeCode());
        }

        if (StringUtils.isNotBlank(transportInvoiceDTOQuery.getShowDate())) {
            query.put("showDate", transportInvoiceDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportInvoiceDTOQuery.getStartDate())) {
            query.put("startDate", transportInvoiceDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportInvoiceDTOQuery.getEndDate())) {
            query.put("endDate", transportInvoiceDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportInvoiceDTOQuery.getOrderBy())) {
            query.put("orderBy", transportInvoiceDTOQuery.getOrderBy());
        }

        if (transportInvoiceDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportInvoiceDTOQuery.getStopFlag());
        }

        List<TransportInvoiceDTO> list = convert(transportInvoiceService.selectByQuery(query));
        Page<TransportInvoiceDTO> page = Page.from(transportInvoiceDTOQuery.getPn(), transportInvoiceDTOQuery.getPageSize(), list);
        page.setTotalItems(transportInvoiceService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportInvoiceService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportInvoice delete failed.", e);
            throw new RuntimeException("TransportInvoice delete failed, Please order your code.", e);
        }
    }
}